package com.mb.app.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mb.app.domain.TbBannerInfo;
import com.mb.app.dao.TbBannerInfoMapper;
import com.mb.app.service.ITbBannerInfoService;
import com.mb.common.config.CommonConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 轮播图  服务实现类
 * </p>
 *
 * @author liliping
 * @since 2019-03-21
 */
@Service
@Slf4j
public class TbBannerInfoServiceImpl extends ServiceImpl<TbBannerInfoMapper, TbBannerInfo> implements ITbBannerInfoService {

    @Autowired
    private CommonConfig commonConfig;

    @Override
    public List<TbBannerInfo> queryList(Map<String, Object> map) {
        List<TbBannerInfo> list = this.baseMapper.lst(map);
        return list;
    }

    @Override
    public int count(Map<String, Object> map) {
        return this.baseMapper.countt(map);
    }

    @Override
    public int save(TbBannerInfo TbBannerInfo) {
        TbBannerInfo.setCreateTime(new Date());
        if(StringUtils.isNotEmpty(TbBannerInfo.getImgUrl())) {
            String 	ImgUrl = TbBannerInfo.getImgUrl();
            ImgUrl = ImgUrl.replaceAll(commonConfig.getImgDomain(),"");
            TbBannerInfo.setImgUrl(ImgUrl);
        }
        return this.baseMapper.insert(TbBannerInfo);
    }

    @Override
    public int update(TbBannerInfo TbBannerInfo) {
        try {
            TbBannerInfo TbBannerInfo1 = new TbBannerInfo();
            TbBannerInfo1.setBannerId(TbBannerInfo.getBannerId());
            TbBannerInfo1.setDataStatus(1);
            TbBannerInfo TbBannerInfoDO = this.baseMapper.selectOne(TbBannerInfo1);
            if(TbBannerInfoDO != null){
                if(StringUtils.isNotEmpty(TbBannerInfo.getImgUrl())) {
                    String 	ImgUrl = TbBannerInfo.getImgUrl();
                    ImgUrl = ImgUrl.replaceAll(commonConfig.getImgDomain(),"");
                    TbBannerInfo.setImgUrl(ImgUrl);
                }
                TbBannerInfo.setUpdateTime(new Date());
                this.baseMapper.updateById(TbBannerInfo);
            }
        } catch (Exception e) {
            log.error("update Exception",e);
        }
        return 0;
    }


    @Override
    public int remove(Long bannerId,Long delUserId) {
//        return this.baseMapper.deleteById(appId);
        TbBannerInfo TbBannerInfo = new TbBannerInfo();
        TbBannerInfo.setBannerId(bannerId);
        TbBannerInfo.setDelTime(new Date());
        TbBannerInfo.setDataStatus(0);
        TbBannerInfo.setDelUserId(delUserId);
        return this.baseMapper.updateById(TbBannerInfo);
    }

    @Override
    public TbBannerInfo getDtl(Long bannerId) {
        TbBannerInfo tbBannerInfo = this.baseMapper.selectByIds(bannerId);
        if(tbBannerInfo != null && StringUtils.isNotEmpty(tbBannerInfo.getImgUrl())) {
            tbBannerInfo.setImgUrl(commonConfig.getImgDomain()+tbBannerInfo.getImgUrl());
        }
        return tbBannerInfo;
    }

    @Override
    public boolean channelNameCodeExit(Map<String, Object> params) {
        boolean exit;
        exit = this.baseMapper.selectByBannerInfo(params).size() > 0;
        return exit;
    }
    @Override
    public int status(Long bannerId,Integer status) {
//        return this.baseMapper.deleteById(appId);
        TbBannerInfo tbBannerInfo = new TbBannerInfo();
        tbBannerInfo.setBannerId(bannerId);
        tbBannerInfo.setStatus(status);
        return this.baseMapper.updateById(tbBannerInfo);
    }



}
